﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace Rotempco.Core.Updater.Objects
{
    public class RotempcoSettings
    {
        private Dictionary<string, string> _KeyValues;
        public string SettingsFile { get; private set; }
        private bool _Persist = true;
          
        public void BundleUpdate(Action action)
        {
            try
            {
                _Persist = false;
                action();
            }
            finally
            {
                _Persist = true;
                SaveSettings();
            }
        }

        public static RotempcoSettings GetProjectSettings(string projectSettingsPath)
        {
            return LoadSettings(projectSettingsPath);
        }


        public void SetKey(string key, string[] values)
        {
            StringBuilder stringBuilder = new StringBuilder();

            foreach (string value in values)
            {
                if (stringBuilder.Length > 0)
                    stringBuilder.Append("«");

                stringBuilder.Append(value);
            }
            SetKey(key, stringBuilder.ToString());
        }

        public void SetKey(string key, object value)
        {
            SetKey(key, value.ToString());
        }

        public void SetKey(string key, string value)
        {
            if (_KeyValues.ContainsKey(key))
                _KeyValues[key] = value;
            else
                _KeyValues.Add(key, value);

            SaveSettings();
        }

        public bool? GetBoolValue(string key)
        {
            bool boolValue = false;

            if (!bool.TryParse(GetValue(key), out boolValue))
                return null;

            return boolValue;
        }


        public string[] GetStringValues(string key)
        {
            string values = GetValue(key);
            List<string> returnList = new List<string>();

            foreach (string value in values.Split('«'))
                returnList.Add(value);

            return returnList.ToArray();
        }

        public int? GetIntValue(string key)
        {
            int intValue = -1;

            if (!int.TryParse(GetValue(key), out intValue))
                return null;

            return intValue;
        }

        public string GetValue(string key)
        {
            if (_KeyValues.ContainsKey(key))
                return _KeyValues[key];

            return "";
        }

        private void SaveSettings()
        {
            if (!_Persist)
                return;

            string tempFile = SettingsFile + ".tmp";

            if (File.Exists(SettingsFile))
                File.Move(SettingsFile, tempFile);

            foreach (string key in _KeyValues.Keys)
                File.AppendAllText(SettingsFile, key + "♥" + _KeyValues[key] + Environment.NewLine);

            File.Delete(tempFile);
        }

        private static RotempcoSettings LoadSettings(string settingsFile)
        {
            RotempcoSettings rotempcoSettings = new RotempcoSettings();
            rotempcoSettings._KeyValues = new Dictionary<string, string>();
            rotempcoSettings.SettingsFile = settingsFile;

            if (File.Exists(settingsFile))
            {
                string[] settings = File.ReadAllLines(settingsFile);

                foreach (string line in settings)
                {
                    string[] elements = line.Split('♥');

                    if (!rotempcoSettings._KeyValues.ContainsKey(elements[0]))
                        rotempcoSettings._KeyValues.Add(elements[0], elements[1]);
                    else
                        rotempcoSettings._KeyValues[elements[0]] = elements[1];
                }
            }

            return rotempcoSettings;
        }

    }
}
